package com.cloudservice.ccs.controller;

import com.cloudservice.ccs.biz.ICustomerServiceBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.entity.CustomerService;
import com.cloudservice.ccs.model.HomeTable;
import com.cloudservice.ccs.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/customer-service")
public class CustomerServiceController {

    @Autowired
    private ICustomerServiceBiz customerServiceBiz;

    // @RequiresPermissions("all")
    @PostMapping("/")
    public ApiResult save(@RequestBody CustomerService customerService) {
        // 接口问题待讨论：是否应当返回生成的 id？
        customerServiceBiz.save(customerService);
        return ApiResult.success();
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}")
    public ApiResult update(@PathVariable Integer id, @RequestBody CustomerService customerService) {
        customerServiceBiz.update(id, customerService);
        CustomerService result = customerServiceBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @PutMapping("/{id}/partial")
    public ApiResult partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields) {
        customerServiceBiz.partialUpdate(id, fields);
        CustomerService result = customerServiceBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @DeleteMapping("/{id}")
    public ApiResult delete(@PathVariable Integer id) {
        customerServiceBiz.delete(id);
        return ApiResult.success();
    }

    // @RequiresPermissions("all")
    @GetMapping("/{id}")
    public ApiResult queryById(@PathVariable Integer id) {
        CustomerService result = customerServiceBiz.getOne(id);
        return ApiResult.successWithData(result);
    }

    // @RequiresPermissions("all")
    @GetMapping("/")
    public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<CustomerService> result = customerServiceBiz.getAll();
        return ApiResult.successWithData(PageData.fromPage((Page<CustomerService>) result));
    }

    @DeleteMapping("/{partial}/deletePartial")
    public ApiResult deletePartial(@PathVariable String partial){
        String []temp=partial.split(",");
        for(String s:temp){
            int id=Integer.parseInt(s);
            customerServiceBiz.delete(id);
        }
        return ApiResult.success();
    }

    @GetMapping("/hometable")
    public ApiResult getHomeTable(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
        PageHelper.startPage(page, limit);
        List<HomeTable> result = customerServiceBiz.getHomeTable();
        return ApiResult.successWithData(PageData.fromPage((Page<HomeTable>) result));
    }
}
